<?php
declare (strict_types=1);

namespace app\course\model\payment;

use app\common\model\payment\PaymentOrderItem as OrderItemCommon;
use app\course\logic\base\Course;

/**
 * 订单模型
 * @method join(string $string, string $string1)
 */
class PaymentOrderItem extends OrderItemCommon
{

    public function course()
    {
        return $this->belongsTo(Course::class, 'product_id')->with(['cover_url'])->bind([
            'product_title'     => 'title',
            'cover_url'         => 'cover_url',
            'product_type_text' => 'course_type_text',
        ]);
    }

    public function scopeRecommendWellSale($query, $time = 0, $limit = 8)
    {
        $query->whereIn('product_type_id', Course::getCourseProducts())->where('update_time', '>=', $time)->group('product_id')->field("product_id as id, count(1) as count")->order('count DESC')->limit($limit);
    }
    public function scopeWellSale($query, $type, $limit = 8)
    {
        $query->whereIn('product_type_id', $type)->group('product_id')->field("product_id as id, count(1) as count")->order('count DESC')->limit($limit);
    }
}
